﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media;
using landlord2.Properties;

namespace landlord2
{
    public class Sound
    {
        private string m_resDir;
        private MediaPlayer m_playerBGM;
        private bool m_bgmRepeat;
        private Queue<MediaPlayer> m_playerSE;
        private List<MediaPlayer> m_playerSEUse;
        private bool? m_enableBGM;
        private bool? m_enableSE;
        private Uri m_currentBGM;
        private int? m_bgm;

        private static readonly string[] BGMResources = new string[]
        {
            "Music/Welcome.mp3",
            "Music/Exciting.mp3",
            "Music/Normal.mp3",
            "Music/Normal2.mp3",
            //"Music/huanqin.mp3"
            "Music/theme1.mp3"
        };

        private static readonly string[] WinLoseResources = new string[]
        {
            "Music/Lose.mp3",
            "Music/Win.mp3",
        };

        private static readonly string[][] ChatSE = new string[][]
        {
            new string[]
            {
                "Sound/chat/1000.mp3",
                "Sound/chat/1001.mp3",
                "Sound/chat/1002.mp3",
                "Sound/chat/1003.mp3",
                "Sound/chat/1004.mp3",
                "Sound/chat/1005.mp3",
                "Sound/chat/1006.mp3",
                "Sound/chat/1007.mp3",
                "Sound/chat/1008.mp3",
                "Sound/chat/1009.mp3",
                "Sound/chat/1010.mp3",
                "Sound/chat/1011.mp3"
            },
            new string[]
            {
                "Sound/chat/2000.mp3",
                "Sound/chat/2001.mp3",
                "Sound/chat/2002.mp3",
                "Sound/chat/2003.mp3",
                "Sound/chat/2004.mp3",
                "Sound/chat/2005.mp3",
                "Sound/chat/2006.mp3",
                "Sound/chat/2007.mp3",
                "Sound/chat/2008.mp3",
                "Sound/chat/2009.mp3",
                "Sound/chat/2010.mp3",
                "Sound/chat/2011.mp3"
            }
        };

        private static readonly string[][] VoiceSE = new string[][]
        {
            new string[]
            {
                "Sound/Man/1.mp3",
                "Sound/Man/2.mp3",
                "Sound/Man/3.mp3",
                "Sound/Man/4.mp3",
                "Sound/Man/5.mp3",
                "Sound/Man/6.mp3",
                "Sound/Man/7.mp3",
                "Sound/Man/8.mp3",
                "Sound/Man/9.mp3",
                "Sound/Man/10.mp3",
                // 10
                "Sound/Man/11.mp3",
                "Sound/Man/12.mp3",
                "Sound/Man/13.mp3",
                "Sound/Man/14.mp3",
                "Sound/Man/15.mp3",
                "Sound/Man/dui1.mp3",
                "Sound/Man/dui2.mp3",
                "Sound/Man/dui3.mp3",
                "Sound/Man/dui4.mp3",
                "Sound/Man/dui5.mp3",
                // 20
                "Sound/Man/dui6.mp3",
                "Sound/Man/dui7.mp3",
                "Sound/Man/dui8.mp3",
                "Sound/Man/dui9.mp3",
                "Sound/Man/dui10.mp3",
                "Sound/Man/dui11.mp3",
                "Sound/Man/dui12.mp3",
                "Sound/Man/dui13.mp3",
                "Sound/Man/sange.mp3",
                "Sound/Man/sandaiyidui.mp3",
                // 30
                "Sound/Man/liandui.mp3",
                "Sound/Man/feiji.mp3",
                "Sound/Man/zhadan.mp3",
                "Sound/Man/wangzha.mp3",
                "Sound/Man/buyao1.mp3",
                "Sound/Man/buyao2.mp3",
                "Sound/Man/buyao3.mp3",
                "Sound/Man/buyao4.mp3",
                "Sound/Man/dani1.mp3",
                "Sound/Man/dani2.mp3",
                // 40
                "Sound/Man/dani3.mp3",
                "Sound/Man/jiaodizhu.mp3",
                "Sound/Man/bujiao.mp3",
                "Sound/Man/qiangdizhu1.mp3",
                "Sound/Man/qiangdizhu2.mp3",
                "Sound/Man/qiangdizhu3.mp3",
                "Sound/Man/buqiang.mp3",
                "Sound/Man/mingpai.mp3",
                "Sound/Man/baojing1.mp3",
                "Sound/Man/baojing2.mp3"
            },
            new string[]
            {
                "Sound/Woman/1.mp3",
                "Sound/Woman/2.mp3",
                "Sound/Woman/3.mp3",
                "Sound/Woman/4.mp3",
                "Sound/Woman/5.mp3",
                "Sound/Woman/6.mp3",
                "Sound/Woman/7.mp3",
                "Sound/Woman/8.mp3",
                "Sound/Woman/9.mp3",
                "Sound/Woman/10.mp3",
                "Sound/Woman/11.mp3",
                "Sound/Woman/12.mp3",
                "Sound/Woman/13.mp3",
                "Sound/Woman/14.mp3",
                "Sound/Woman/15.mp3",
                "Sound/Woman/dui1.mp3",
                "Sound/Woman/dui2.mp3",
                "Sound/Woman/dui3.mp3",
                "Sound/Woman/dui4.mp3",
                "Sound/Woman/dui5.mp3",
                "Sound/Woman/dui6.mp3",
                "Sound/Woman/dui7.mp3",
                "Sound/Woman/dui8.mp3",
                "Sound/Woman/dui9.mp3",
                "Sound/Woman/dui10.mp3",
                "Sound/Woman/dui11.mp3",
                "Sound/Woman/dui12.mp3",
                "Sound/Woman/dui13.mp3",
                "Sound/Woman/sange.mp3",
                "Sound/Woman/sandaiyidui.mp3",
                "Sound/Woman/liandui.mp3",
                "Sound/Woman/feiji.mp3",
                "Sound/Woman/zhadan.mp3",
                "Sound/Woman/wangzha.mp3",
                "Sound/Woman/buyao1.mp3",
                "Sound/Woman/buyao2.mp3",
                "Sound/Woman/buyao3.mp3",
                "Sound/Woman/buyao4.mp3",
                "Sound/Woman/dani1.mp3",
                "Sound/Woman/dani2.mp3",
                "Sound/Woman/dani3.mp3",
                "Sound/Woman/jiaodizhu.mp3",
                "Sound/Woman/bujiao.mp3",
                "Sound/Woman/qiangdizhu1.mp3",
                "Sound/Woman/qiangdizhu2.mp3",
                "Sound/Woman/qiangdizhu3.mp3",
                "Sound/Woman/buqiang.mp3",
                "Sound/Woman/mingpai.mp3",
                "Sound/Woman/baojing1.mp3",
                "Sound/Woman/baojing2.mp3"
            }
        };

        private static readonly string[] SEResources = new string[]
        {
            "Sound/SE/fapai.mp3",
            "Sound/SE/start.mp3",
            "Sound/SE/chupai.mp3",
            "Sound/SE/da.mp3",
            "Sound/SE/boom.mp3",
            "Sound/SE/boom2.mp3",
            "Sound/SE/alarm.mp3"
        };

        public Sound()
        {
            m_resDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources");
            m_playerBGM = new MediaPlayer();
            m_playerBGM.MediaEnded += PlayerBGM_MediaEnded;
            m_playerBGM.Volume = 0.6;
            m_playerSE = new Queue<MediaPlayer>();
            m_playerSEUse = new List<MediaPlayer>();
        }

        public void PlayBGM(int index)
        {
            if (index >= BGMResources.Length) throw new Exception();
            Uri uri = new Uri(Path.Combine(m_resDir, BGMResources[index]));
            PlayBGM(uri);
        }

        public void PlayBGM(Uri uri)
        {
            m_currentBGM = uri;
            if (EnableBGM)
            {
                m_playerBGM.Open(uri);
                m_playerBGM.Play();
            }
            m_bgmRepeat = true;
        }

        public void PlayResult(bool win)
        {
            int index = win ? 1 : 0;
            Uri uri = new Uri(Path.Combine(m_resDir, WinLoseResources[index]));
            if (EnableBGM)
            {
                m_playerBGM.Open(uri);
                m_playerBGM.Play();
            }
            m_bgmRepeat = false;
        }

        public void StopBGM()
        {
            m_playerBGM.Stop();
        }

        private void PlayerBGM_MediaEnded(object sender, EventArgs e)
        {
            if (!m_bgmRepeat) return;
            m_playerBGM.Position = new TimeSpan();
            m_playerBGM.Play();
        }

        public void PlaySE(Uri uri)
        {
            if (EnableSE)
            {
                MediaPlayer player;
                if (m_playerSE.Count == 0)
                {
                    player = new MediaPlayer();
                    player.MediaEnded += PlayerSE_MediaEnded;
                    m_playerSEUse.Add(player);
                }
                else player = m_playerSE.Dequeue();
                player.Open(uri);
                player.Play();
            }
        }

        private void PlayerSE_MediaEnded(object sender, EventArgs e)
        {
            m_playerSE.Enqueue((MediaPlayer)sender);
        }

        public void PlayChat(int gender, int index)
        {
            PlaySE(new Uri(Path.Combine(m_resDir, ChatSE[gender][index])));
        }

        public void PlayVoice(int gender, int index)
        {
            PlaySE(new Uri(Path.Combine(m_resDir, VoiceSE[gender][index])));
        }

        public void PlaySE(int index)
        {
            PlaySE(new Uri(Path.Combine(m_resDir, SEResources[index])));
        }

        public int BGM
        {
            get
            {
                if (m_bgm == null) m_bgm = Settings.Default.BGM;
                return m_bgm.Value;
            }
            set
            {
                m_bgm = value;
                PlayBGM(m_bgm.Value);

                Settings.Default.BGM = value;
                Settings.Default.Save();
            }
        }

        public bool EnableBGM
        {
            get
            {
                if (m_enableBGM == null) m_enableBGM = Settings.Default.EnableBGM;
                return m_enableBGM.Value;
            }
            set
            {
                m_enableBGM = value;
                if (value && m_bgmRepeat) PlayBGM(m_currentBGM);
                else StopBGM();

                Settings.Default.EnableBGM = value;
                Settings.Default.Save();
            }
        }

        public bool EnableSE
        {
            get
            {
                if (m_enableSE == null) m_enableSE = Settings.Default.EnableSE;
                return m_enableSE.Value;
            }
            set
            {
                m_enableSE = value;

                Settings.Default.EnableSE = value;
                Settings.Default.Save();
            }
        }
    }
}
